package com.junxonline.instant.admin.controller.account;

import cn.hutool.core.util.StrUtil;
import com.junxonline.instant.dao.entity.dto.common.UserInfoDTO;
import com.junxonline.instant.dao.entity.vo.account.ChangePasswordVO;
import com.junxonline.instant.dao.entity.vo.account.LoginVO;
import com.junxonline.instant.admin.service.config.UserService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 账号相关Controller
 *
 * @author JunX
 * @date 2021-06-24
 */
@Log4j2
@RestController
@RequestMapping(value = "/")
public class AccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 登录
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @OperationLog(label = "登录", type = OperationEnum.OTHER)
    @PostMapping(value = "/login")
    public CommonResult login(@RequestBody(required = false) LoginVO vo) {
        if (StrUtil.isBlank(vo.getUsername()) || StrUtil.isBlank(vo.getPassword())) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_NOT_MATCH);
        }
        UserInfoDTO userInfoDTO = userService.login(vo.getUsername(), vo.getPassword());
        return CommonResult.success(userInfoDTO);
    }

    /**
     * 注销
     *
     * @return CommonResult
     */
    @OperationLog(label = "注销", type = OperationEnum.OTHER)
    @RequiresAuthentication
    @PostMapping(value = "/logout")
    public CommonResult logout() {
        String token = request.getHeader("Authorization");
        userService.logout(token);
        return CommonResult.success();
    }

    /**
     * 获取用户菜单
     *
     * @return CommonResult
     */
    @OperationLog(label = "获取用户菜单", type = OperationEnum.QUERY)
    @RequiresAuthentication
    @PostMapping(value = "/getRouters")
    public CommonResult getRouters() {
        String token = request.getHeader("Authorization");
        return CommonResult.success(userService.getRouters(token));
    }

    /**
     * 登录
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @OperationLog(label = "修改密码", type = OperationEnum.EDIT)
    @PostMapping(value = "/changePassword")
    public CommonResult changePassword(@RequestBody(required = false) ChangePasswordVO vo) {
        if (StrUtil.isBlank(vo.getOldPassword()) || StrUtil.isBlank(vo.getNewPassword())) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_NOT_MATCH);
        }
        String token = request.getHeader("Authorization");
        userService.changePassword(token, vo.getOldPassword(), vo.getNewPassword());
        return CommonResult.success();
    }

}
